<?php
class BannerAction extends CommonAction {

    public function index() {
        $bn = D('Banner');
        //搜索模块
        parent::search(array(
            'name'=>'广告名字',
            'url'=>'广告url'
        ));
        if(!isset($_GET['act'])) {
            parent::page($bn->count());
            $allBanner = $bn->field(array('id', 'name', 'link', 'is_active', 'type'))
                           ->order('type ASC,is_active DESC,date DESC')
                           ->limit($this->limit)
                           ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($bn->where($this->map)->count());
            $allBanner = $bn->field(array('id', 'name', 'link', 'is_active', 'type'))
                            ->order('type ASC,is_active DESC,date DESC')
                            ->where($this->map)
                            ->limit($this->limit)
                            ->select();
        }
        foreach($allBanner as $key=>&$value) {
            if($value['type'] == 1) $value['type'] = '<font style="color:green">网站顶部广告</font>';
            if($value['type'] == 2) $value['type'] = '<font style="color:#f60">网站轮播广告</font>';
        }
        $this->assign(array(
            'empty'=>'<tr><td colspan="6">没有任何数据</td></tr>',
            'allbanner'=>$allBanner
        ));
        $this->display();
    }

    public function add() {
        if(isset($_POST['send'])) {
            $bn = D('Banner');
            if($bn->create()) {
                $bn->add() ? $this->success('添加广告成功!', __URL__) : $this->error('系统出错，广告添加失败!', 0);
            } else {
                $this->error($bn->getError(), 0);
            }
        }
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $bn = D('Banner');
            if(isset($_POST['send'])) {
                if($bn->create()) {
                    $bn->save() ? $this->success('修改广告成功!', __URL__) : $this->error('系统出错，修改广告失败!', 0);
                } else {
                    $this->error($bn->getError());
                }
            }
            if(!$bn->field('id')->where('id='.$id)->find()) $this->error('不存在此数据!', 0);
            $banner = $bn->field(array('id', 'name', 'link', 'is_active', 'pic', 'type'))->where('id='.$id)->find();
            if($banner['type'] == 1) {
                $banner['type'] = '网站顶部广告';
            } else if($banner['type'] == 2) {
                $banner['type'] = '网站轮播广告';
            }
            $this->assign('onebanner', $banner);
            $this->display();
        }

    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $bn = D('Banner');
            if(!$bn->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $bn->where('id='.$id)->delete() ? $this->success('删除广告成功!', __URL__) : $this->error('系统出错，删除广告失败!', 0);
        }
    }

    public function mulDelete() {
        $bn = D('Banner');
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $map['id'] = array('in', $ids);
        $bn->where($map)->delete() ? $this->success('批量删除广告成功!', __URL__) : $this->error('系统出错，批量删除广告失败!', 0);
    }

}

?>